package dead;

/**
 * 
 * synchronized产生的死锁分析
 * jstack -l 1228  可以分析线程死锁问题，看出哪两个处于死锁状态
 * 
 * @author lcx
 *
 */
public class SynchronizedDeadLock implements Runnable{

    private static Object o1=new Object();
    private static Object o2=new Object();

    private static boolean flag=false;
    public void m1()
    {
        synchronized(o1)
        {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("m1-1");
            synchronized(o2)
            {
                System.out.println("m1-2");
            }
        }

    }

    public void m2()
    {
        synchronized(o2)
        {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.out.println("m2-1");
            synchronized(o1)
            {
                System.out.println("m2-2");
            }
        }
    }

    public static void main(String[] args) {
        SynchronizedDeadLock dl=new SynchronizedDeadLock();
        Thread thd1=new Thread(dl);
        Thread thd2=new Thread(dl);
        thd1.start();
        thd2.start();
    }

    public void run() {
        if(!flag)
         {
            flag=true;
            m1();
            return;
         }
         m2();
    }
}

